package com.cysion.study.ui

import android.text.TextUtils
import android.view.View
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.other.color
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.adapter.ChooseKnowListAdapter
import com.cysion.study.entity.KnowledgeListEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.utils.CLog
import com.cysion.study.net.ApiClient
import com.cysion.study.widget.ShowToast
import kotlinx.android.synthetic.main.activity_modify_know.*

//编辑知识点
class ModifyKnowActivity : BaseActivity(), View.OnClickListener {


    override fun getLayoutId(): Int = R.layout.activity_modify_know
    private lateinit var dataAdapter: ChooseKnowListAdapter
    private val dataList: MutableList<String> = mutableListOf()
    private var curPage = 1
    private var courseId=""
    private var type=""//2新加，1编辑 知识点id，修改的时候必须传
    private var obj: KnowledgeListEntity? = null

    override fun initView() {
        courseId = intent.extras.getString("courseId")
        type = intent.extras.getString("type")
        whiteTextTheme(color(R.color.main_cl))
         topbar.apply {
             if (type == "2") {
                 setTitle("添加知识点")
             } else {
                 setTitle("编辑知识点")
                 obj = intent.extras.getSerializable("obj") as KnowledgeListEntity?
                 initModify()

             }
//            setTexts("添加", TopBar.Pos.RIGHT)
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }else if (pos == TopBar.Pos.RIGHT) {
//                    startActivity_ex<AddKnowActivity>()//添加习题与知识

                    /*AlertSearch.search(self,""){ confirm: Int, start: String, end: String ->
                        CLog.i("@@@搜索="+start+end)
                        curPage=1
                        getDataList(curPage,start,end)
                    }*/
                }
            }
        }

        tv_submit.setOnClickListener(this)
        iv_del.setOnClickListener(this)

    }

    private fun initModify() {
        et_title.setText(obj?.title)
        et_content.setText(obj?.content)
        cb_item.isChecked = obj?.ifStore == "1"
        ifStore = obj?.ifStore.toString()
    }

    private var content=""
    private var ifStore="1"//是否存储到知识库：1、是；不传就代表不存储
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_submit->{
                var title = et_title.text.toString().trim()
                 content = et_content.text.toString().trim()
                if (TextUtils.isEmpty(title)) {
                    ShowToast("请输入标题")
                    return
                }
                if (TextUtils.isEmpty(content)) {
                    ShowToast("请输入内容")
                    return
                }
                if (cb_item.isChecked) {
                    ifStore = "1"
                } else {
                    ifStore = ""
                }
                submit(title)
            }
            R.id.iv_del->{
                et_content.setText("")
                content = ""

            }
        }
    }

    private fun submit(title: String) {
        Alert.loading(self)
        var id=""//知识点id，修改的时候必须传
        //type 2新加，1编辑
        if (type == "1") {
            id=obj?.id.toString()
        }
        ApiClient.api.knowledgesaveOrUpdate(UserCache.token, id, courseId, title, content, ifStore)
            .compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    ShowToast("添加成功")
                    finish()
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)

                }
            }

    }




    override fun closeMvp() {
    }

}
